//
//  NSString+DM_MD5.m
//  24-加密
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "NSString+DM_MD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (DM_MD5)

- (NSString *)stringWith32Bit {
    // 1.将字符串转化成C语言字符串
    const char *cString = [self UTF8String];
    // 2.将C的字符串转换 MD5
    // CC_MD5(<#const void *data#>, <#CC_LONG len#>, <#unsigned char *md#>)
    // 参数1:const void *data   任意指针类型
    // 参数2:CC_LONG len        字符串长度
    // 参数3:unsigned char *md
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cString, length, bytes);
    // 将C的字符串转换成OC的字符串
    NSMutableString *finalString = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    // 循环遍历
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        // 遍历拼接成OC的字符串
        [finalString appendFormat:@"%02x", bytes[i]];
    }
    return finalString;

}
- (NSString *)stringWith32Bit_MD5Lower {
    return [self stringWith32Bit].lowercaseString;
}
- (NSString *)stringWith32Bit_MD5Upper {
    return [self stringWith32Bit].uppercaseString ;
}
- (NSString *)stringWith16Bit_MD5 {
    
    NSString *string = [self stringWith32Bit];
    // 截掉字符串的后8位
    NSString *subString = [string substringToIndex:(string.length - 8)];
    // 截掉字符串的前八位
    NSString *subToString = [subString substringFromIndex:8];
    // 截取字符串从第九位起16长度的字符串
//    NSString *string2 = [string substringWithRange:NSMakeRange(8, 16)];
    
    return subToString;
}
- (NSString *)stringWith16Bit_MD5Lower {
    return [self stringWith16Bit_MD5].lowercaseString;
}
- (NSString *)stringWith16Bit_MD5Upper {
    return [self stringWith16Bit_MD5].uppercaseString;
}
@end
